package com.strive.demo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.strive.common.model.base.Result;
import com.strive.demo.web.model.PageInfoModel;
import com.strive.demo.web.model.ShopModel;
import com.strive.demo.web.service.IShopService;
import com.strive.fastdfs.model.StorePathModel;
import com.strive.demo.web.vo.ShopInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @description:
 * @author: bingcun.chen
 * @Date: 2022/3/10 15:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/form")
@Api(tags = "商品信息")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 查询所有数据
     * webflux 初试
     * @return
     */
    @PostMapping("/queryShopInfo")
    @ApiOperation("demo-查询所有")
    public Mono<Result<List<ShopModel>>> queryShopInfo(){
        return Mono.just(Result.succeed(shopService.queryShopInfo()));
    }

    /**
     * demo分页数据
     * @param pageInfo
     * @return
     */
    @PostMapping("/queryShopInfoPage")
    @ApiOperation("demo-查询分页")
    public Mono<Result<Page<ShopModel>>> queryShopInfoPage(@RequestBody PageInfoModel pageInfo) throws InterruptedException {
        Page<ShopModel> shopModelPage = shopService.queryShopInfoPage(pageInfo);
        Thread.sleep(10000);
        return Mono.just(Result.succeed(shopModelPage));
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/deleteShopInfo")
    @ApiOperation("demo-删除数据")
    public Mono<Result<Boolean>> deleteShopInfo(@RequestParam("id") Integer id){
        return Mono.just(Result.succeed(shopService.deleteShopInfo(id)));
    }

    /**
     * 初始化数据
     * @return
     */
    @PostMapping("/initShopInfo")
    @ApiOperation("demo-初始化测试数据")
    public Mono<Result<Boolean>> initShopInfo(@RequestParam("nums") Integer nums){
        return Mono.just(Result.succeed(shopService.initShopInfo(nums)));
    }

    /**
     * 上传
     * @return
     */
    @PostMapping("/uploadFile")
    @ApiOperation("demo-添加商品包含上传")
    @ApiImplicitParam(name = "file", value = "多个图片",  allowMultiple = true, dataType = "MultipartFile", paramType = "query")
    public Mono<Result<StorePathModel>> uploadFile(@RequestParam(value = "file")MultipartFile thumbnail){
        return Mono.just(Result.succeed(shopService.uploadFile(thumbnail)));
    }

    /**
     * 删除图片
     * @return
     */
    @PostMapping("/deleteShopFile")
    @ApiOperation("demo-删除图片")
    public Mono<Result<Boolean>> deleteShopFile(@RequestParam(value = "path") String path){
        return Mono.just(Result.succeed(shopService.deleteShopFile(path)));
    }

    /**
     * 下载文件
     */
    @PostMapping("/downLoadFiles")
    @ApiOperation("demo-下载文件")
    public void downLoadFiles(@RequestParam("id") Integer id,HttpServletResponse response) throws IOException {
        shopService.downLoadFiles(id,response);
    }

    /**
     * 展示文件缩略图
     */
    @PostMapping("/showShopImg")
    @ApiOperation("demo-展示缩略图")
    public Mono<Result<List<String>>> showShopImg(@RequestParam("id") Integer id){
        return Mono.just(Result.succeed(shopService.showShopImg(id)));
    }

    /**
     * 导入数据
     * @param file
     * @return
     */
    @PostMapping("/importFile")
    @ApiOperation("导入数据")
    public Mono<Result<String>> importFile(@RequestParam("file") MultipartFile file){
        return Mono.just(Result.succeed(shopService.importFile(file)));
    }

    /**
     * 添加数据
     * @param shopInfoVo
     * @return
     */
    @PostMapping("/addShopInfo")
    public Mono<Result<Boolean>> addShopInfo(@RequestBody ShopInfoVo shopInfoVo){
        boolean flag = shopService.addShopInfo(shopInfoVo);
        return Mono.just(Result.succeed(flag));
    }
}
